package cn.nexd.app.arsenal.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import cn.nexd.app.NexdApplication;
import cn.nexd.app.arsenal.R;

/**
 * Created by codngfish on 16/2/2.
 */
public class SettingFragment extends DialogFragment implements CompoundButton.OnCheckedChangeListener {
    CheckBox wifiCheckBox;
    CheckBox beaconCheckBox;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setting_fragment, container, false);
        this.getDialog().setTitle("设置采集方式");
        wifiCheckBox = (CheckBox) view.findViewById(R.id.clooector_wifi);
        beaconCheckBox = (CheckBox) view.findViewById(R.id.clooector_bluetooth);
        // 当wifi 和 beacon 同时为 false 时, 设置 wifi 和 beacon 为选中状态
        if (NexdApplication.mix) {
            wifiCheckBox.setChecked(true);
            beaconCheckBox.setChecked(true);
        } else {
            //
            if (NexdApplication.wifi) {
                wifiCheckBox.setChecked(true);
            } else {
                wifiCheckBox.setChecked(false);
            }

            if (NexdApplication.bluetooth) {
                beaconCheckBox.setChecked(true);
            } else {
                beaconCheckBox.setChecked(false);
            }

        }
        wifiCheckBox.setOnCheckedChangeListener(this);
        beaconCheckBox.setOnCheckedChangeListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.clooector_wifi:
                if (isChecked) {
                    NexdApplication.wifi = true;
                } else {
                    NexdApplication.wifi = false;
                }
                checkChecked();
                break;
            case R.id.clooector_bluetooth:
                if (isChecked) {
                    NexdApplication.bluetooth = true;
                } else {
                    NexdApplication.bluetooth = false;
                }
                checkChecked();
                break;
        }
    }

    void checkChecked() {
        if (wifiCheckBox.isChecked() && beaconCheckBox.isChecked()) {
            NexdApplication.mix = true;
            NexdApplication.wifi = false;
            NexdApplication.bluetooth = false;
        } else if (wifiCheckBox.isChecked()) {
            NexdApplication.wifi = true;
            NexdApplication.mix = false;
        } else if (beaconCheckBox.isChecked()) {
            NexdApplication.bluetooth = true;
            NexdApplication.mix = false;
        }
    }
}
